<?php require_once('Connections/db_conn.php'); ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<?php

$part_id=$_GET['id'];
$workshop_id = $_GET['wid'];
$from_event = $_GET['event'];
mysql_select_db($database_db_conn, $db_conn);
$query_Participants = "SELECT * FROM participants WHERE PARTICIPANTID = $part_id";
$Participants= mysql_query($query_Participants, $db_conn) or die(mysql_error());
$row_Participants = mysql_fetch_assoc($Participants);

$query_Language = "SELECT lang.LANGID LANGID, lang.LANG LANG, partp.LANGID CHK FROM `language` lang
	left join `participantlanguage` partp on lang.LANGID = partp.LANGID and partp.participantid=$part_id
	ORDER BY lang.LANGID";
$Language = mysql_query($query_Language, $db_conn) or die(mysql_error());
$query_Ethnicity = "SELECT etn.ETHNICITYID ETHNICITYID , etn.ETHNICITY ETHNICITY, partp.ETHNICITYID CHK FROM `ETHNICITY` etn
	left join `participantethnicity` partp on etn.ETHNICITYID = partp.ETHNICITYID and partp.participantid=$part_id
	ORDER BY etn.ETHNICITYID";
$Ethnicity = mysql_query($query_Ethnicity, $db_conn) or die(mysql_error());
$query_PartType = "SELECT tp.TYPEID TYPEID, tp.TYPE TYPE, partp.TYPEID CHK  FROM `TYPE` tp left join `participanttype` partp ".
	"on tp.TYPEID = partp.TYPEID and partp.participantid=$part_id ORDER BY tp.TYPEID";
$type = mysql_query($query_PartType, $db_conn) or die(mysql_error());
$query_Workshops = "SELECT w.WRKSPID, w.WRKSPDATE, w.WRKSPTIME, w.LOCATION, wt.WRKSPTOPIC, l.LANG from workshop w, workshoptopics
	wt, language l, workshopparticipants wp, participants p where w.WRKSPID=wp.WRKSPID and wp.PARTICIPANTID=p.PARTICIPANTID and
	l.LANGID=w.WRKSPLANGID and wt.WRKSPTOPICID=w.WRKSPTOPICID and p.PARTICIPANTID='$part_id'";
$workshops = mysql_query($query_Workshops, $db_conn) or die(mysql_error());

$layout = explode('&&&', file_get_contents('layout.html'));
echo $layout[0];

session_start();
if (!(isset($_SESSION['username'])))
	header("location:login.php");


?>

<html>
<body>

<!-- Begin Main Column -->

<div id="mainContent">

	<h2>Edit Participant</h2>
  <form id="form1" method="post" action="">
    <table width="402" border="0">
      <tr>
        <td width="121">First Name:</td>
        <td width="271"><label>
            <input type="text" name="first_name" id="first_name" value="<?php echo $row_Participants['FIRSTNAME']?>" />
        </label></td>
      </tr>
      <tr>
        <td>Last Name:</td>
        <td><label>
            <input type="text" name="last_name" id="last_name" value="<?php echo $row_Participants['LASTNAME']?>" />
        </label></td>
      </tr>
      <tr>
        <td>Address:</td>
        <td><label>
            <input type="text" name="address" id="address" value="<?php echo $row_Participants['ADDRESS']?>" />
        </label></td>
      </tr>
      <tr>
        <td>City:</td>
        <td><label>
            <input type="text" name="city" id="city" value="<?php echo $row_Participants['CITY']?>" />
        </label></td>
      </tr>
      <tr>
        <td>Zip Code:</td>
        <td><label>
            <input name="zipcode" type="text" id="zipcode" size="5" maxlength="5" value="<?php echo $row_Participants['ZIPCODE']?>" />
        </label></td>
      </tr>
      <tr>
        <td>Phone Number:</td>
        <td><label>
            <input name="phone_number" type="text" id="phone_number" size="10" maxlength="10" value="<?php echo $row_Participants['PHONENO']?>" />
        </label></td>
      </tr>
      <tr>
        <td>Email:</td>
        <td><label>
            <input type="text" name="email" id="email" value="<?php echo $row_Participants['EMAIL']?>" />
        </label></td>
      </tr>
      <tr>
        <td height="33">Participant Type:</td>
        <td>
	    	<?php
				 while ($row_type = mysql_fetch_assoc($type))
				 {
				 	$type_chk = ($row_type['CHK'] == NULL)?"unchecked":"checked";
				 	echo "<input type='checkbox' name='part_type[]' value='".$row_type['TYPEID']."' $type_chk />   ".$row_type['TYPE']."<br />";
				  }
				 mysql_free_result($type);
	  		?>
            Other:
        <input type="text" name="type_other" id="type_other" />
        </td>
      </tr>
      <tr>
        <td height="36">Gender:</td>
        <td>
        	<input type="radio" name="gender" value="Male" id="male_gender" <?php echo ($row_Participants['GENDER'] == "Male")?"checked":"unchecked"; ?> />Male
			<input type="radio" name="gender" value="Female" id="female_gender" <?php echo ($row_Participants['GENDER'] == "Female")?"checked":"unchecked"; ?> />Female
        </td>
      </tr>
      <tr>
        <td>Younger than 20:</td>
        <td>
			<input type="radio" name="youngerthan20" value="Yes" id="younger_than_20_yes" <?php echo ($row_Participants['YOUNGERTHAN20'] == "Yes")?"checked":"unchecked"; ?> />Yes
			<input type="radio" name="youngerthan20" value="No" id="younger_than_20_no" <?php echo ($row_Participants['YOUNGERTHAN20'] == "No")?"checked":"unchecked"; ?> />No

		</td>
      </tr>
      <tr>
        <td height="28">Ethnicity:</td>
        <td>
			<?php
				while ($row_ethc = mysql_fetch_assoc($Ethnicity))
				{
					$etn_ck = ($row_ethc['CHK'] == NULL)?"unchecked":"checked";
					echo "<input type='checkbox' name='ethnicity[]' value='".$row_ethc['ETHNICITYID']."' $etn_ck  />   ".$row_ethc['ETHNICITY']."<br />";
				}
				mysql_free_result($Ethnicity);
	  		?>
            Other:
        <input type="text" name="ethnicity_other" id="ethnicity_other" />
      </tr>
      <tr>
        <td>Number of Children:</td>
        <td><label>
            <input type="text" name="number_of_children" id="number_of_children" value="<?php echo $row_Participants['NUMBEROFCHILDREN']?>" />
        </label></td>
      </tr>
      <tr>
        <td>Children's Age:</td>
        <td>
        	<input type="checkbox" name="months_0_12" id="months_0_12" <?php echo ($row_Participants['MONTHS_0_12'] == "1")?"checked":"unchecked"; ?> />0-12 Months<br />
            <input type="checkbox" name="months_13_23" id="months_13_23" <?php echo ($row_Participants['MONTHS_13_23'] == "1")?"checked":"unchecked"; ?> />13-23 Months<br />
  	        <input type="checkbox" name="years_2_4" id="years_2_4" <?php echo ($row_Participants['YEARS_2_4'] == "1")?"checked":"unchecked"; ?> />2-4 Years<br />
  	        <input type="checkbox" name="years_5_6" id="years_5_6" <?php echo ($row_Participants['YEARS_5_6'] == "1")?"checked":"unchecked"; ?> />5-6 Years<br />
  	        <input type="checkbox" name="years_7_8" id="years_7_8" <?php echo ($row_Participants['YEARS_7_8'] == "1")?"checked":"unchecked"; ?> />7-8 Years<br />
  	        <input type="checkbox" name="over_8" id="over_8" <?php echo ($row_Participants['YEARS_OVER_8'] == "1")?"checked":"unchecked"; ?> />Over 8 Years</td>
      </tr>
      <tr>
        <td>Has Disabilities:</td>
        <td>
			<input type="radio" name="disabilities" value="Yes" id="disabilities_yes" <?php echo ($row_Participants['HASDISABILITIES'] == "Yes")?"checked":"unchecked"; ?> />Yes
			<input type="radio" name="disabilities" value="No" id="disabilities_no" <?php echo ($row_Participants['HASDISABILITIES'] == "No")?"checked":"unchecked"; ?> />No
		</td>
      </tr>
      <tr>
        <td>Languages:</td>
        <td>
		  <?php
			  while ($row_Language = mysql_fetch_assoc($Language))
			  {
			  	$etn_lang = ($row_Language['CHK'] == NULL)?"unchecked":"checked";
			  	echo "<input type='checkbox' name='part_lang[]' value='".$row_Language['LANGID']."' $etn_lang />   ".$row_Language['LANG']."<br />";
			  }
			  mysql_free_result($Language);
		  ?>
          Other:
        <input type="text" name="language_other" id="language_other" />
        </td>
      </tr>
      <tr>
        <td>Send Newsletter:</td>
        <td>
        	<input type="radio" name="newsletter" value="Yes" id="newsletter_yes" <?php echo ($row_Participants['SENDNEWSLETTER'] == "Yes")?"checked":"unchecked"; ?> />Yes
			<input type="radio" name="newsletter" value="No" id="newsletter_no" <?php echo ($row_Participants['SENDNEWSLETTER'] == "No")?"checked":"unchecked"; ?> />No
        </td>
      </tr>
      <tr>
        <td>Send Member Information:</td>
        <td>
        	<input type="radio" name="member_info" value="Yes" id="member_info_yes" <?php echo ($row_Participants['SENDMEMBERINFO'] == "Yes")?"checked":"unchecked"; ?> />Yes
			<input type="radio" name="member_info" value="No" id="member_info_no" <?php echo ($row_Participants['SENDMEMBERINFO'] == "No")?"checked":"unchecked"; ?> />No
        </td>
      </tr>
      </table>
      <p>
	    <label>
	    <input type="submit" name="update" id="update" value="Update" class = "button" />
        </label>
	  </p>
  </form>

<?php
echo "Workshops Participated in:<br>";
echo "<div style='width:700px;overflow:auto;'>";
echo "<table border='1'>";
echo "	<tr>
			<th>Workshop ID</th>
			<th>Date</th>
			<th>Time</th>
			<th>Location</th>
			<th>Topic</th>
			<th>Language</th>
		</tr>";

//keeps getting the next row until there are no more to get
while($row = mysql_fetch_array($workshops))
{
	// Print out the contents of each row into a table
	echo "<tr><td>";
	echo "<a href=\"EditWorkshop.php?id=".$row['WRKSPID']."\">".$row['WRKSPID']."</a>";
	echo "</td><td>";
	echo date("m/j/y", strtotime($row['WRKSPDATE']));
	echo "</td><td>";
	$hours=strval(substr($row['WRKSPTIME'],0,2));
	if($hours>12)
	{
		$hours=$hours-12;
		$tod="pm";
	}
	else
		$tod="am";
	$mins=strval(substr($row['WRKSPTIME'],3,2));
	echo $hours.":".$mins."".$tod;
	echo "</td><td>";
	echo $row['LOCATION'];
	echo "</td><td>";
	echo $row['WRKSPTOPIC'];
	echo "</td><td>";
	echo $row['LANG'];
	echo "</td></tr>";

}
echo "</table></div><br/><br/>";

if (isset($_POST['update']))  // if page is not submitted to itself echo the form
{
	ob_start();

	//Checking input
	$first_name = $_POST["first_name"];
	$last_name = $_POST["last_name"];
	$address = $_POST["address"];
	$gender = $_POST["gender"];
	if ( $first_name != ""  && $last_name != "")
	{
		$id = $_POST["participant_id"];
		$city = $_POST["city"];
		$zipcode = $_POST["zipcode"];
		$phone_number = $_POST["phone_number"];
		$email = $_POST["email"];
		$part_type = $_POST["part_type"];
		$youngerthan20 = $_POST["youngerthan20"];
		$ethnicity_type = $_POST["ethnicity"];
		$number_of_children = $_POST["number_of_children"];
		$child_age = $_POST["child_age"];
		$disabilities = $_POST["disabilities"];
		$part_lang = $_POST["part_lang"];
		$newsletter = $_POST["newsletter"];
		$memberinfo = $_POST["member_info"];
		$months_0_12 = $_POST["months_0_12"];
		$months_13_23 = $_POST["months_13_23"];
		$years_2_4 = $_POST["years_2_4"];
		$years_5_6 = $_POST["years_5_6"];
		$years_7_8 = $_POST["years_7_8"];
		$over_8 = $_POST["over_8"];
		$language_other = $_POST['language_other'];
		$ethnicity_other = $_POST['ethnicity_other'];
		$type_other = $_POST['type_other'];

		$first_name = stripslashes($first_name);
		$last_name = stripslashes($last_name);
		$address = stripslashes($address);
		$city = stripslashes($city);
		$zipcode = stripslashes($zipcode);
		$phone_number = stripslashes($phone_number);
		$email = stripslashes($email);
		$number_of_children = stripslashes($number_of_children);
		$language_other = stripslashes($language_other);
		$ethnicity_other = stripslashes($ethnicity_other);
		$type_other = stripslashes($type_other);

		$first_name = mysql_real_escape_string($first_name);
		$last_name = mysql_real_escape_string($last_name);
		$address = mysql_real_escape_string($address);
		$city = mysql_real_escape_string($city);
		$zipcode = mysql_real_escape_string($zipcode);
		$phone_number = mysql_real_escape_string($phone_number);
		$email = mysql_real_escape_string($email);
		$number_of_children = mysql_real_escape_string($number_of_children);
		$ethnicity_other = mysql_real_escape_string($ethnicity_other);
		$type_other = mysql_real_escape_string($type_other);
		$language_other = mysql_real_escape_string($language_other);

		if($number_of_children == "" )
			$number_of_children = 0;

	}

	if($months_0_12 == "on")
		$months_0_12 = "1";
	else
		$months_0_12 = "0";
	if($months_13_23 == "on")
		$months_13_23 = "1";
	else
		$months_13_23 = "0";
	if($years_2_4 == "on")
		$years_2_4 = "1";
	else
		$years_2_4 = "0";
	if($years_5_6 == "on")
		$years_5_6 = "1";
	else
		$years_5_6 = "0";
	if($years_7_8 == "on")
		$years_7_8 = "1";
	else
		$years_7_8 = "0";
	if($over_8 == "on")
		$over_8 = "1";
	else
		$over_8 = "0";
	// Connect to the database server
	mysql_select_db($database_db_conn, $db_conn);

	// add it to the database.
	$sql = "UPDATE `participants` SET ".
	"`FIRSTNAME` = '$first_name',".
	"`LASTNAME` = '$last_name',".
	"`ADDRESS` = '$address',".
	"`CITY` = '$city',".
	"`ZIPCODE` = '$zipcode',".
	"`PHONENO` = '$phone_number',".
	"`EMAIL` = '$email',".
	"`GENDER` = '$gender',".
	"`YOUNGERTHAN20` = '$youngerthan20',".
	"`NUMBEROFCHILDREN` = '$number_of_children',".
	"`HASDISABILITIES` = '$disabilities',".
	"`MONTHS_0_12` = '$months_0_12',".
	"`MONTHS_13_23` = '$months_13_23',".
	"`YEARS_2_4` = '$years_2_4',".
	"`YEARS_5_6` = '$years_5_6',".
	"`YEARS_7_8` = '$years_7_8',".
	"`YEARS_OVER_8` = '$over_8',".
	"`SENDNEWSLETTER` = '$newsletter',".
	"`SENDMEMBERINFO` = '$memberinfo'".
	" WHERE PARTICIPANTID = $part_id ";
	if (mysql_query($sql, $db_conn))
	{
		//Update participant language
		$lang_delete = "DELETE FROM PARTICIPANTLANGUAGE WHERE PARTICIPANTID = '$part_id'";
		mysql_query($lang_delete, $db_conn);
		if (!empty($part_lang))
		{
			foreach ($part_lang as $k => $v)
			{
				$sql_lang = "INSERT INTO PARTICIPANTLANGUAGE (LANGID , PARTICIPANTID ) VALUES ($v,$part_id)";
				mysql_query($sql_lang, $db_conn) or die('Error adding Language: $sql_lang mysql_error()');
			}
		}

		$query_Language = "SELECT LANG FROM language";
		$Language = mysql_query($query_Language, $db_conn) or die(mysql_error());
		if($language_other != NULL)
		{
			while ($row_Language = mysql_fetch_assoc($Language)){
				if($row_Language['LANG'] == $language_other)
				{
					$lang_yes = 1;
					break;
				}
				else
				{
					$lang_yes = 0;
				}
			}
			if($lang_yes == 0)
			{
				mysql_query("INSERT INTO language (LANG) VALUES ('$language_other')");
				$get_lang_id=mysql_query("select@@IDENTITY");
				$lang_id=mysql_result($get_lang_id,0);
				echo "INSERT INTO PARTICIPANTLANGUAGE (LANGID , PARTICIPANTID ) VALUES ($lang_id,$part_id)";
				mysql_query("INSERT INTO PARTICIPANTLANGUAGE (LANGID , PARTICIPANTID ) VALUES ($lang_id,$part_id)");
			}
		}

		// Update Ethinicity
		$sql_etn="DELETE FROM `participantethnicity` WHERE PARTICIPANTID = '$part_id'";
		mysql_query($sql_etn, $db_conn);
		if (!empty($ethnicity_type ))
		{
			foreach ($ethnicity_type as $k => $v)
			{
				$sql_lang = "INSERT INTO `participantethnicity` (`ETHNICITYID` , `PARTICIPANTID` ) VALUES ($v,$part_id)";
				mysql_query($sql_lang, $db_conn) or die('Error adding Ethinicity: $sql_lang mysql_error()');
			}
		}
		$query_Ethnicity = "SELECT ETHNICITY FROM ethnicity";
		$Ethnicity = mysql_query($query_Ethnicity, $db_conn) or die(mysql_error());
		if($ethnicity_other != NULL)
		{
			while ($row_Ethnicity = mysql_fetch_assoc($Ethnicity)){
				if($row_Ethnicity['ETHNICITY'] == $ethnicity_other)
				{
					$eth_yes = 1;
					break;
				}
				else
				{
					$eth_yes = 0;
				}
			}
			if($eth_yes == 0)
			{
				mysql_query("INSERT INTO ethnicity (ETHNICITY) VALUES ('$ethnicity_other')");
				$get_eth_id=mysql_query("select@@IDENTITY");
				$eth_id=mysql_result($get_eth_id,0);
				echo "INSERT INTO PARTICIPANTETHNICITY(ETHNICITYID , PARTICIPANTID )
					VALUES ($eth_id,$part_id)";
				mysql_query("INSERT INTO PARTICIPANTETHNICITY(ETHNICITYID , PARTICIPANTID )
					VALUES ($eth_id,$part_id)");
			}
		}

		// Update Participant Type
		$sql_type = "DELETE FROM `participanttype` WHERE PARTICIPANTID = '$part_id'";
		mysql_query($sql_type, $db_conn);
		if (!empty($part_type))
		{
			foreach ($part_type as $k => $v)
			{
				$sql_lang = "INSERT INTO `participanttype` (`TYPEID` , `PARTICIPANTID` ) VALUES ($v,$part_id)";
				mysql_query($sql_lang, $db_conn) or die('Error adding Type: $sql_lang mysql_error()');
			}
		}
		$query_Type = "SELECT TYPE FROM type";
		$Type = mysql_query($query_Type, $db_conn) or die(mysql_error());
		if($type_other != NULL)
		{
			while ($row_Type = mysql_fetch_assoc($Type)){
				if($row_Type['TYPE'] == $type_other)
				{
					$type_yes = 1;
					break;
				}
				else
				{
					$type_yes = 0;
				}
			}
			if($type_yes == 0)
			{
				mysql_query("INSERT INTO type (TYPE) VALUES ('$type_other')");
				$get_type_id=mysql_query("select@@IDENTITY");
				$type_id=mysql_result($get_type_id,0);
				echo "INSERT INTO PARTICIPANTTYPE(TYPEID , PARTICIPANTID ) VALUES ($type_id,$part_id)";
				mysql_query("INSERT INTO PARTICIPANTTYPE(TYPEID , PARTICIPANTID ) VALUES ($type_id,$part_id)");
			}
		}
		if($from_event) {
			echo "<META HTTP-EQUIV='refresh' content='0;URL=EditEvent.php?id=$workshop_id'>";
		}


		if($workshop_id != "")
		{
			mysql_query("INSERT INTO workshopparticipants(WRKSPID, PARTICIPANTID, NUMBEROFCHILDREN)
				VALUES($workshop_id, $part_id, $number_of_children)");
			echo "<META HTTP-EQUIV='refresh' content='0;URL=EditEvent.php?id=$workshop_id'>";
		}
		else
			echo("<P>Participant has been updated successfully.</P>");
			//echo "<META HTTP-EQUIV='refresh' content='3;URL=EditParticipant.php?id=$part_id'>";
	}
	else
	{
		echo("<P>Error adding participating: $id $sql" .
				 mysql_error() . "</P>");
	}


	mysql_close();
}
ob_end_flush();


?>
</div>

<!-- Begin Side Column -->
<!-- Begin Footer -->

<?php
echo $layout[1];
?>

</body>
</html>